/*
@todo
 1. 新增 UUID 的兩個方法，分別為 setSQLUUIDRandom(SQLMaker), setSQLUUIDNamebase(SQLMaker, String)。

History
 2013-01-13：
 1. 修改 setSQLNO(SQLMaker) method 改用 JDK 5 建議的時間方法 System.nanoTime() 以取得更精準的序號，此序號在目前的電腦運算速度將不再重複。
*/
package idv.jieh.commons.sql;

import idv.jieh.web.html.Selection;
import idv.jieh.commons.comm.BasicClass;
import idv.jieh.commons.util.CurrentDate;

public class ColumnFactory extends BasicClass {

  protected ColumnAdapter column;
  protected int type;

  public ColumnFactory(ColumnAdapter column, int type){
    this.column = column;
    this.type = type;
  }

  public void addSQLWhere(SQLMaker sm, String value){
    sm.appendWhere(type, column, value);
  }

  public void addSQLWhere(SQLMaker sm, RowCache row){
    addSQLWhere(sm, getRowValue(row));
  }

  public void addSQLDesignatedWhere(SQLMaker sm, String value){
    sm.appendDesignatedWhere(type, column, value);
  }

  public void addSQLDesignatedWhere(SQLMaker sm, RowCache row){
    addSQLDesignatedWhere(sm, getRowValue(row));
  }

  public void addSQLWhereDateRange(
    SQLMaker sm,
    String startDate,
    String endDate)
  {
    addSQLWhereDateRange(sm, startDate, endDate, true, true);
  }

  public void addSQLWhereDateRange(
    SQLMaker sm,
    String startDate,
    String endDate,
    boolean startDesignated,
    boolean endDesignated)
  {
    sm.OP2 = ">=";
    if(startDesignated)
      addSQLDesignatedWhere(sm, startDate);
    else
      addSQLWhere(sm, startDate);

    sm.OP2 = "<=";
    if(endDesignated)
      addSQLDesignatedWhere(sm, endDate);
    else
      addSQLWhere(sm, endDate);

    sm.OP2 = "=";
  }

  public void setSQLValue(SQLMaker sm, String value){
    sm.setValue(type, column, value);
  }

  public String setSQLNO(SQLMaker sm){
    String sno = String.valueOf(System.nanoTime());
    sm.setValue(type, column, sno);
    return sno;
  }
/*
  public String setSQLUUIDRandom(SQLMaker sm){
    String uuid = java.util.UUID.randomUUID().toString();
    sm.setValue(type, column, uuid);
    return uuid;
  }

  public String setSQLUUIDNamebase(SQLMaker sm, String name){
    String uuid = java.util.UUID.nameUUIDFromBytes(name.getBytes()).toString();
    sm.setValue(type, column, uuid);
    return uuid;
  }
*/
  public String setSQLDate(SQLMaker sm){
    String date = new CurrentDate().getCrntDate(CurrentDate.AD, "-");
    setSQLValue(sm, date);
    return date;
  }

  public String setSQLTime(SQLMaker sm){
    String time = new CurrentDate().getCrntTime(":", true);
    setSQLValue(sm, time);
    return time;
  }

  public void setSQLValue(SQLMaker sm, RowCache row){
    setSQLValue(sm, getRowValue(row));
  }

  public String getRowValue(RowCache row){
    return row.getValue(column);
  }

  public void setRowValue(RowCache row, String value) {
    row.setValue(value, column.name());
  }

  public String getSelectionText(Selection slt, RowCache row) {
    return slt.getText(getRowValue(row));
  }

} // end class ColumnFactory
